package com.example.workbench.common.config.shiro;

import com.example.workbench.common.api.CommonResult;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Component
public class UserUtil {

    @Autowired
    private RedisUtil redisUtil;
    public static HttpServletRequest getHttpServletRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }
    public String getToken() {
        return getHttpServletRequest().getHeader("token");
    }
    public Long getUserId() {
    String token = getToken();
        TokenEntity entity = (TokenEntity) redisUtil.get(token);
        return entity.getUserId();
    }
    public String getUsername() {
        String token = getToken();
        TokenEntity entity = (TokenEntity) redisUtil.get(token);
        return entity.getUsername();
    }
}

